using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Data.OleDb;

namespace DAOLayer
{
    public class ChiTietPhieuThueDao
    {
        public static void Them(ChiTietPhieuThueDto ctpt)
        {
            // B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
            OleDbConnection cn = Database.ConnectionData();
            // B3: Tao chuoi strSQL thao tac CSDL
            string strSQL = "Insert into CHITIETPHIEUTHUE ( MaPhieuThue,TenKhachHang,MaLoaiKhach,CMND,DiaChi ) values(?,?,?,?,?)";
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
            cmd.Parameters.Add("@MaPhieuThue", OleDbType.Integer);
            cmd.Parameters["@MaPhieuThue"].Value = ctpt.MaPhieuThue;
            cmd.Parameters.Add("@TenKhachHang", OleDbType.WChar);
            cmd.Parameters["@TenKhachHang"].Value = ctpt.TenKhachHang;
            cmd.Parameters.Add("@MaLoaiKhach", OleDbType.Integer);
            cmd.Parameters["@MaLoaiKhach"].Value = ctpt.MaLoaiKhach;
            cmd.Parameters.Add("@CMND", OleDbType.Integer);
            cmd.Parameters["@CMND"].Value = ctpt.CMND;
            cmd.Parameters.Add("@DiaChi", OleDbType.WChar);
            cmd.Parameters["@DiaChi"].Value = ctpt.DiaChi;
            cmd.ExecuteNonQuery();

            strSQL = "Select @@IDENTITY";
            cmd = new OleDbCommand(strSQL, cn);
            ctpt.MaChiTietPhieu= Convert.ToInt32(cmd.ExecuteScalar());
            // B5: Dong ket noi CSDL
            cn.Close();
        }
    }
}
